package com.me.first;

import java.util.Iterator;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class First implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;
	Texture nyan;
	Texture mydpad;
	Texture background;
	Texture rainbow;
	Texture broc;
	Music nyanMusic;
	Rectangle cat;
	Sprite sprite;
	Rectangle control;
	Rectangle down, up, left, right;
	Rectangle poop;
	Rectangle broccoli;
	Mesh mesh;
	Array<Rectangle> poobows;
	Array<Rectangle> broccolis;
	long lastDropTime;
	long lastDropTime1;
	BitmapFont font;
	CharSequence str = "0";
	int count = 0;

	
	
	
	@Override
	public void create() {		
		Texture.setEnforcePotImages(false);
		//float w = Gdx.graphics.getWidth();
		//float h = Gdx.graphics.getHeight();
		
		/*if (mesh == null) {
             mesh = new Mesh(true, 3, 3, 
                     new VertexAttribute(Usage.Position, 3, "a_position"));          

             mesh.setVertices(new float[] { -0.5f, -0.5f, 0,
                                            0.5f, -0.5f, 0,
                                            0, 0.5f, 0 });   
             mesh.setIndices(new short[] { 0, 1, 2 });                       
		}*/

		
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 800, 480);
		batch = new SpriteBatch();
		
		nyanMusic = Gdx.audio.newMusic(Gdx.files.internal("data/nyansong.mp3"));
		
		nyanMusic.setLooping(true);
		nyanMusic.play();
		
		font = new BitmapFont();
		
		//explosion = Gdx.audio.newSound(Gdx.files.internal("data/boom.wav"));
		
		nyan = new Texture(Gdx.files.internal("data/NyanCat.png"));
		//nyan.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		//mydpad = new Texture(Gdx.files.internal("data/d-pad.png"));
		//mydpad.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		background = new Texture(Gdx.files.internal("data/stars.png"));
		
		rainbow = new Texture(Gdx.files.internal("data/rainbow.png"));
		
		broc = new Texture(Gdx.files.internal("data/broccoli.png"));
		//TextureRegion region = new TextureRegion(nyan, 0, 0, 193, 128);
		
		
		
		/*control = new Rectangle();
		control.x = 0;
		control.y = 0;
		control.width = 200;
		control.height = 200;*/
		
		
		
		
		cat = new Rectangle();
		cat.x = (800-210);
		cat.y = 20;
		cat.width = 193;
		cat.height = 128;
		
		poobows = new Array<Rectangle>();
		broccolis = new Array<Rectangle>();
		
		
		
		
		
		//control.set(50, 50, 50);
		
		//sprite = new Sprite(nyan, 193, 128);
		//sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
		//sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
		//sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
	}
	
	private void spawnPoop() {
	      poop = new Rectangle(0,0,200,200);
	      poop.x = (800-210) - 170;
	      poop.y = cat.y;
	      
	      poobows.add(poop);
	      lastDropTime = TimeUtils.nanoTime();
	   }
	
	private void spawnBroccoli() {
	      broccoli = new Rectangle(0,0,80,80);
	      broccoli.y = MathUtils.random(0, 480-64);
	      broccoli.x = 0;
	     
	      broccolis.add(broccoli);
	      lastDropTime1 = TimeUtils.nanoTime();
	   }


	

	@Override
	public void render() {
		//float w = Gdx.graphics.getWidth();
		//float h = Gdx.graphics.getHeight();
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		camera.update();
		
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		batch.draw(background, 0, 0);
		batch.draw(nyan, cat.x, cat.y);
		batch.draw(rainbow, cat.x - 170, cat.y);
		font.draw(batch, str, 750, 480);
		//batch.draw(mydpad, control.x, control.y);
		for(Rectangle poop: poobows) {
	         batch.draw(rainbow, poop.x, poop.y);
	      }
		for(Rectangle broccoli: broccolis) {
	         batch.draw(broc, broccoli.x, broccoli.y);
	      }
		
		batch.end();
		
		
       
		
		Vector3 touchPos = new Vector3();
		if(Gdx.input.isTouched()) {
	         
	         touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
	         camera.unproject(touchPos);
	         if (touchPos.y > cat.y){
	        	 cat.y += 200 * Gdx.graphics.getDeltaTime();
	         }
	         else if (touchPos.y < cat.y) {
	        	 cat.y -= 200 * Gdx.graphics.getDeltaTime();
	         }
	         
	     
		}
		
		
	         
	         
	       
		
		
		if(cat.x < 0) {
			cat.x = 0;
		}
	    if(cat.x > 800 - 64) {
	    	cat.x = 800 - 64;
	    }
	    
	    if(TimeUtils.nanoTime() - lastDropTime > 1000000000) {
	    	spawnPoop();
	    }
	    
	    if(TimeUtils.nanoTime() - lastDropTime1 > 1000000000) {
	    	spawnBroccoli();
	    }
		
		/*if(cat.x < 0) {
			cat.x = 0;
		}
	    if(cat.x > w) {
	    	cat.x = w;
	    }
	    if(cat.y < 0) {
			cat.y = 0;
		}
	    if(cat.y > h) {
	    	cat.y = h;
	    }*/
	    
	    Iterator<Rectangle> iter = poobows.iterator();
	      while(iter.hasNext()) {
	         Rectangle poop = iter.next();
	         poop.x -= 200 * Gdx.graphics.getDeltaTime();
	         if(poop.x < 0) {
	        	 iter.remove();
	         }
	      }
	      
	     
	      
	     Iterator<Rectangle> iter1 = broccolis.iterator();
	      while(iter1.hasNext()) {
	         Rectangle broccoli = iter1.next();
	         broccoli.x += 200 * Gdx.graphics.getDeltaTime();
	         if (broccoli.x > 800) {
	        	 iter1.remove();
	         }
	         if (poop.overlaps(broccoli) || broccoli.overlaps(poop)) {
	        	 iter1.remove();
	        	 count += 1;
	        	 str = Integer.toString(count);
	        	 //explosion.play();
	        	 
	        	 
	         }
	         //if (broccoli.overlaps(cat)) {
	         //Gdx.gl.glClearColor(0, 0, 0, 1);
	    	  	//Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	         //}
	        	
	         }
	}
	
	@Override
	public void dispose() {
		batch.dispose();
		nyan.dispose();
		broc.dispose();
		nyanMusic.dispose();
		background.dispose();
		mydpad.dispose();
		rainbow.dispose();
		
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}
